﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Telerik.Web.UI;
using SMA.GENTEv1;

public partial class Protegidos_Empresas_Cadastros__popup_ecpIncluirConvencaoAditivo : PageBase 
{
    protected int idConvencao = 0;
    private const string dbPath = "arq/";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            OcultaCampos(true);
        }

        if (Request.QueryString["IdConvencao"] != null)
            idConvencao = int.Parse(Request.QueryString["IdConvencao"].ToString());
    }

    #region Eventos
    protected void btnGravar_Click(object sender, EventArgs e)
    {
        try
        {
            BindValidResults();
        }
        catch (Exception ex)
        {
            lblResultado.Text = ex.Message;
        }
    }

    protected void btnFechar_Click(object sender, EventArgs e)
    {
        ClientScript.RegisterStartupScript(Page.GetType(), "mykey", "CancelEdit();", true);
    }
    #endregion

    #region Metodos
    private void BindValidResults()
    {
        if (RadUpload1.UploadedFiles.Count > 0)
        {
            string nameFile = Salvar();

            foreach (UploadedFile validFile in RadUpload1.UploadedFiles)
            {
                string targetFolder = Server.MapPath("~/Arq");
                validFile.SaveAs(Path.Combine(targetFolder, nameFile), true);
            }

            labelNoResults.Visible = false;
            repeaterResults.Visible = true;
            repeaterResults.DataSource = RadUpload1.UploadedFiles;
            repeaterResults.DataBind();
        }
        else
        {
            labelNoResults.Visible = true;
            repeaterResults.Visible = false;
        }

        ExibeResultado();
    }

    private void ExibeResultado()
    {
        trSeleciona.Visible = false;
        trResultado.Visible = true;
        trMsg.Visible = false;
        btnGravar.Visible = false;

        trTitulo.Visible = false;
        trInicio.Visible = false;
        trTermino.Visible = false;
    }

    protected void OcultaCampos(bool oculta)
    {
        trResultado.Visible = !oculta;
    }

    protected string Salvar()
    {
        ConvencaoAditivo aditivo = new ConvencaoAditivo();

        aditivo.IdConvencaoAditivo = 0;
        aditivo.Convencao = new Convencao(idConvencao, true, true, false);
        aditivo.Descricao = txtTitulo.Text;
        if (dtInicio.SelectedDate != null)
            aditivo.DtInicio = DateTime.Parse(dtInicio.SelectedDate.ToString());
        if (dtTermino.SelectedDate != null)
            aditivo.DtFim = DateTime.Parse(dtTermino.SelectedDate.ToString());
        aditivo.Path = dbPath;
        aditivo.Salvar();

        FileInfo _fileInfo = new FileInfo(aditivo.Path);

        return _fileInfo.Name;
    }
    #endregion
}
